

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jy
 */
//閺堬拷銇囧锝呮倻閸栧綊鍘�
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MaxMatching implements Segment {
    private int m_nMaxLength = 3; //閺堬拷銇囬崠褰掑帳鐠囧秹鏆�姒涙顓绘稉锟�
    private int m_nPositionIndex = 0; //鐠囧秶娈戝〒鍛婄垼閹稿洭鎷�
    private StringBuilder m_result = new StringBuilder();
    
    public MaxMatching(int maxLength)
    {
        m_nMaxLength = maxLength;
    }
    
    public MaxMatching() {}
    
    public void segment(String str)
    {
        maxMatching(str, m_nMaxLength, 0);
    }
    
    private void maxMatching(String str, int matchLength, int fromPosition)
    {
        
        if(fromPosition >= str.length())
            return ;
        //閸撯晙缍戠�妤冾儊娑撹尙娈戦梹鍨婢堆傜艾3閻ㄥ嫯鐦�
        String tmp;
        if(fromPosition+matchLength <= str.length())
        {
            tmp = str.substring(fromPosition, fromPosition+matchLength);
        }
        else
        {
            matchLength = str.length()-fromPosition;
            tmp = str.substring(fromPosition, fromPosition+matchLength);
        }
        if(Dic.matchWord(tmp))
        {
            //閸栧綊鍘ら幋鎰
            m_result.append(tmp+"/ ");
            fromPosition += tmp.length();
            matchLength = m_nMaxLength;
            maxMatching(str, matchLength, fromPosition);
        }
        else
        {
            //閸栧綊鍘ゆ径杈Е
            if(matchLength > 1)
            {
                matchLength-=1;
                maxMatching(str, matchLength, fromPosition);
            }
            else
            {
                //鐎涙鍚�稉顓濈瑝鐎涙ê婀拠銉ㄧ槤
                m_result.append(" "+tmp);
                fromPosition+=1;
                matchLength=m_nMaxLength;
                maxMatching(str, matchLength, fromPosition);
            }
            
        }
    }
    
    
    public String getResult()
    {
        return m_result.toString();
    }
    

}
